package com.doctor.windflower_doctor.view;


import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.myuser.tools.BitmapHelp;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.PopupWindow;

/**
 * <p>
 * 功能
 * </p>
 * 
 * <p>
 * Copyright 掌动乾坤公司 2014 All right reserved.
 * </p>
 * 
 * @author 宋洁琳 时间 2014-5-13 下午2:58:17
 * @version 1.0 </br> 最后修改人 无
 * 
 */
public class ImagePopUtil implements OnClickListener {
	private Activity activity;
	private static PopupWindow popupWindow;
	private LayoutInflater layoutInflater;
	private int windowWidth;
	private int windowHeight;
	private ViewTreeObserver viewTreeObserver;
	private DragImageView dragImageView;

	public ImagePopUtil(Activity activity, int windowWidth, int windowHeight) {

		// TODO Auto-generated constructor stub
		this.activity = activity;
		layoutInflater = LayoutInflater.from(activity);
		if (popupWindow == null) {
			popupWindow = createPopupWindow();
		}
		this.windowWidth = windowWidth;
		this.windowHeight = windowHeight;
	}

	private PopupWindow createPopupWindow() {
		View contentView = layoutInflater.inflate(R.layout.pop_showimage, null);
		PopupWindow popupWindow = new PopupWindow(contentView,
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.setFocusable(true);
		popupWindow.setAnimationStyle(R.style.style_pop);
		return popupWindow;
	}

	private void setPath(String path) {
		dragImageView = (DragImageView) popupWindow.getContentView()
				.findViewById(R.id.div_main);
		popupWindow.getContentView().findViewById(R.id.imagetouch_bk)
				.setOnClickListener(this);
		BitmapHelp.getBitmapUtils(activity).display(dragImageView, path);
		// 设置图片
		// dragImageView.setImageBitmap(bmp);
		dragImageView.setmActivity(activity);// 注入Activity.
		/** 测量状态栏高度 **/
		viewTreeObserver = dragImageView.getViewTreeObserver();
		viewTreeObserver
				.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
					private int state_height;

					@Override
					public void onGlobalLayout() {
						if (state_height == 0) {
							// 获取状况栏高度
							Rect frame = new Rect();
							activity.getWindow().getDecorView()
									.getWindowVisibleDisplayFrame(frame);
							state_height = frame.top;
							dragImageView.setScreen_H(windowHeight
									- state_height);
							dragImageView.setScreen_W(windowWidth);
						}

					}
				});
	}

	public void showImagePopupWindow(String path) {
		setPath(path);
		popupWindow.showAtLocation(activity.getWindow().getDecorView(),
				Gravity.CENTER, 0, 50);
	}

	public void dismiss() {
		popupWindow.dismiss();
	}

	public boolean isShowing() {
		return popupWindow.isShowing();
	}

	@Override
	public void onClick(View v) {

		// TODO Auto-generated method stub
		popupWindow.dismiss();
	}
}
